package Lianxi.ATM;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

/**
 * 转账功能
 */
public class Transfer extends JPanel implements ActionListener {
    private JButton returnBut; //返回
    private JButton confirm;  //确认
    private JTextField tarnMoney;//转账金额
    private JTextField tarnId;
    private Atm frame;
    private int sum = 0;

    public Transfer(Atm frame) {

        this.frame = frame;

        this.setLayout(null); //空布局

        this.setOpaque(false); //背景透明


        returnBut = new JButton("返回");
        returnBut.setBounds(350, 580, 100, 50);
        this.add(returnBut);
        returnBut.addActionListener(this);

        //确认按钮,确认取款金额
        confirm = new JButton("确认");
        confirm.setBounds(350, 500, 100, 50);
        this.add(confirm);
        confirm.addActionListener(this);

        tarnMoney = new JTextField(); //转账金额文本框
        tarnMoney.setBounds(320, 220, 100, 30);
        this.add(tarnMoney);
        tarnMoney.addActionListener(this);

        tarnId = new JTextField(); //转账户名文本框
        tarnId.setBounds(320, 260, 100, 30);
        this.add(tarnId);
        tarnId.addActionListener(this);


        JLabel label = new JLabel("转入金额：");
        label.setBounds(260, 220, 80, 30);
        this.add(label);

        JLabel label1 = new JLabel("转入账户：");
        label1.setBounds(260, 260, 80, 30);
        this.add(label1);


    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == returnBut) {
            frame.switchPanel("菜单");

        }
        try { //处理账户非法异常
            if (e.getSource() == confirm) {

                String a = tarnId.getText(); //获取转入账户
                int b = Integer.valueOf(tarnMoney.getText()); //获取转入账户金额
                for (int i = 0; i < frame.getUsers().size(); i++) {
                    if (a.equals(frame.getLoginUser().getId())) {//不能给自己转账
                        JOptionPane.showMessageDialog(this, "账户有误");
                        break;
                    }
                    if (a.equals(frame.getUsers().get(i).getId())) {//匹配账号
                        sum++;  //匹配到，sum自增1.


                        if (b <= frame.getLoginUser().getMoney()) {  //判断余额是否足够
                            frame.getLoginUser().setMoney(frame.getLoginUser().getMoney() - b); //转出金额
                            frame.getUsers().get(i).setMoney(frame.getUsers().get(i).getMoney() + b); //对象账户转入金额
                            frame.data(); //序列化保存
                            JOptionPane.showMessageDialog(this, "转账成功！");
                            break;

                        } else {
                            JOptionPane.showMessageDialog(this, "账户余额不足！");
                            break;

                        }


                    }


                }


                if (sum == 0) {   //当计数器为0，证明没有匹配到
                    JOptionPane.showMessageDialog(this, "找不到该账户");
                    return;
                }

            }


        } catch (Exception p) {
            JOptionPane.showMessageDialog(this, "非法账户");
        }


    }
}










